package com.shinewind.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.common.util.FastDFSClientUtil;
import com.shinewind.home.service.IModelAdminService;
import com.shinewind.info.service.IInfoAdminService;
import com.shinewind.pojo.dto.AdminModelDto;
import com.shinewind.pojo.entity.Brand;
import com.shinewind.pojo.entity.Model;
import com.shinewind.pojo.entity.ModelInfo;
import com.shinewind.pojo.entity.ModelMapping;
import com.shinewind.pojo.result.Result;
import com.shinewind.pojo.vo.AdminModelInfoVo;
import com.shinewind.pojo.vo.AdminModelListVo;
import com.shinewind.pojo.vo.AdminUserListVo;
import com.shinewind.pojo.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Api(value = "后台模型接口",description = "模型模块管理")
public class ModelsController {
    @DubboReference
    private IModelAdminService iModelAdminService;
    @DubboReference
    private IInfoAdminService iInfoAdminService;
    @Autowired(required = false)
    private FastDFSClientUtil fastDFSClientUtil;
    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @GetMapping("/model/brand/list")
    @ApiOperation(value = "查询所有品牌信息",notes = "品牌信息",response = Result.class)
    public PageVo<Brand> getBrandListByPage(@RequestParam(value = "current",required = true) Integer current,
                                                          @RequestParam(value = "pageSize",required = true) Integer pageSize){
        //System.out.println("contentType:"+role);
        PageVo<Brand> brandList = iModelAdminService.getModelBrandList(current,pageSize);
        return brandList;
    }

    @GetMapping("/model/brand/keyword/list")
    @ApiOperation(value = "按关键字查询所有品牌信息",notes = "关键字查询品牌信息",response = Result.class)
    public PageVo<Brand> getBrandListByPageAndKeyWord(@RequestParam(value = "current",required = true) Integer current,
                                            @RequestParam(value = "pageSize",required = true) Integer pageSize,
                                                      @RequestParam(value = "keyword",required = false) String keyWord){
        //System.out.println("keyWord:"+keyWord);
        PageVo<Brand> brandList = iModelAdminService.getModelBrandListByKeyWord(current,pageSize,keyWord);
        return brandList;
    }

    @GetMapping("/model/list")
    @ApiOperation(value = "查询所有用户信息",notes = "用户所有信息",response = Result.class)
    public PageVo<AdminModelListVo> getModelListByCondition(@RequestParam(value = "current",required = true) Integer current,
                                                           @RequestParam(value = "pageSize",required = true) Integer pageSize,
                                                           @RequestParam(value = "number",required = false) String title,
                                                           @RequestParam(value = "name",required = false) String typeName,
                                                           @RequestParam(value = "contentType",required = false) String saleTime,
                                                           @RequestParam(value = "filterType",required = false) String price){
        //System.out.println("contentType:"+role);
        PageVo<AdminModelListVo> modelListVoList = iModelAdminService.getModelList(current,pageSize,title,
                typeName,saleTime,price);
        return modelListVoList;
    }

    @GetMapping("/model/detail/{mId}")
    @ApiOperation(value = "根据模型id查询用户详细信息",notes = "查询模型详细信息",response = Result.class)
    public Result getModelInfoByMid(@PathVariable("mId") String mId){
        System.out.println("mId:"+mId);
        Result result = iModelAdminService.getAdminModelDetailByMid(mId);
        return result;
    }

    @GetMapping("/model/type/list")
    @ApiOperation(value = "查询模型类别列表",notes = "查询模型类别",response = Result.class)
    public Result getModelTypeList(){
        Result result = iInfoAdminService.getModelTypeList();
        return result;
    }

    @PostMapping("/model/manual/upload")
    @ApiOperation(value = "上传模型说明书",notes = "上传模型说明书",response = Result.class)
    public Result uploadUserAvatar(MultipartFile file) throws IOException {
        System.out.println("fileName:"+file.getOriginalFilename());
        System.out.println("size:"+file.getSize());
        System.out.println("stream:"+file.getInputStream());
        System.out.println("fileServerUrl:"+fileServerUrl);
        if(file == null){
            throw new RuntimeException("文件不存在");
        }
        else{
            //上传说明书
            String fileId = fastDFSClientUtil.uploadFile(file);
            System.out.println("fileId:"+fileId);
            String fileUrl = fileServerUrl + fileId;
            if(StrUtil.isNotBlank(fileUrl)){
                return Result.success(fileUrl,true,messageConstant.UPLOAD_SUCCESS);
            }
            else{
                return Result.fail(false, messageConstant.UPDATE_ERROR);
            }
        }
    }

    @PostMapping("/model/pictures/upload")
    @ApiOperation(value = "上传模型宣传图",notes = "上传模型宣传图",response = Result.class)
    public Result uploadModelPictures(MultipartFile file) throws IOException {
        System.out.println("fileName:"+file.getOriginalFilename());
        System.out.println("size:"+file.getSize());
        System.out.println("stream:"+file.getInputStream());
        System.out.println("fileServerUrl:"+fileServerUrl);
        if(file == null){
            throw new RuntimeException("文件不存在");
        }
        else{
            //上传说明书
            String fileId = fastDFSClientUtil.uploadFile(file);
            System.out.println("fileId:"+fileId);
            String fileUrl = fileServerUrl + fileId;
            if(StrUtil.isNotBlank(fileUrl)){
                return Result.success(fileUrl,true,messageConstant.UPLOAD_SUCCESS);
            }
            else{
                return Result.fail(false, messageConstant.UPDATE_ERROR);
            }
        }
    }

    //后台接口,上传关联视频
    @PostMapping("/model/video/upload")
    @ApiOperation(value = "模型关联视频上传",notes = "模型关联视频上传",response = Result.class)
    public Result uploadRelateVideo(@PathVariable("mId") String mId,MultipartFile file) throws IOException {
        System.out.println("fileName:"+file.getOriginalFilename());
        System.out.println("size:"+file.getSize());
        System.out.println("stream:"+file.getInputStream());
        System.out.println("fileServerUrl:"+fileServerUrl);
        if(file == null){
            throw new RuntimeException("文件不存在");
        }
        else{
            //上传关联视频
            String fileId = fastDFSClientUtil.uploadFile(file);
            System.out.println("fileId:"+fileId);
            String fileUrl = fileServerUrl + fileId;
            if(StrUtil.isNotBlank(fileUrl)){
                return Result.success(fileUrl,true,messageConstant.UPLOAD_SUCCESS);
            }
            else{
                return Result.fail(false, messageConstant.UPDATE_ERROR);
            }
        }
    }

    @PostMapping("/model/add")
    @ApiOperation(value = "添加模型信息",notes = "添加模型",response = Result.class)
    public Result addUserInfo(@RequestBody AdminModelDto adminModelDto) throws Exception {
        System.out.println("adminModelDto:"+adminModelDto);
        //将信息写入model表中
        Model model = iModelAdminService.addModel(adminModelDto);
        //将信息写入modelInfo表中
        Result result = iInfoAdminService.addModelInfo(model,adminModelDto);
        return result;
    }

    @GetMapping("/model/info/{mId}")
    @ApiOperation(value = "根据模型id查询用户详细信息",notes = "查询模型详细信息",response = Result.class)
    public Result getModelDetailInfoByMid(@PathVariable("mId") String mId){
        System.out.println("mId:"+mId);
        //查询model表中的结果
        Model model = iModelAdminService.getModelInfo(mId);
        //查询modelInfo表中的结果
        ModelInfo modelInfo = iInfoAdminService.getModelInfo(mId);
        //查询status中的结果
        List<Map<String, Object>> status = iModelAdminService.getStatusByMid(mId);
        AdminModelInfoVo adminModelInfoVo = AdminModelInfoVo.builder().build();
        BeanUtils.copyProperties(model,adminModelInfoVo);
        BeanUtils.copyProperties(modelInfo,adminModelInfoVo);
        adminModelInfoVo.setSaleTime(status);
        if(ObjectUtil.isNotNull(adminModelInfoVo)){
            return Result.success(adminModelInfoVo,true,messageConstant.QUERY_SUCCESS);
        }
        return Result.fail(false,messageConstant.QUERY_ERROR);
    }

    @PostMapping("/model/update/{mId}")
    @ApiOperation(value = "修改模型信息",notes = "修改模型",response = Result.class)
    public Result updateModelInfo(@PathVariable("mId") String mId,@RequestBody AdminModelDto adminModelDto) throws Exception {
        System.out.println("adminModelDto:"+adminModelDto);
        //将信息写入model表中
        Boolean flag = iModelAdminService.updateModel(mId,adminModelDto);
        if(flag){
            //将信息写入modelInfo表中
            Result result = iInfoAdminService.updateModelInfo(mId,adminModelDto);
            return result;
        }
        return Result.fail(false,messageConstant.UPDATE_ERROR);
    }

    @DeleteMapping("/model/delete/{mId}")
    @ApiOperation(value = "删除模型信息",notes = "根据模型id删除模型信息",response = Result.class)
    public Result deleteModelByMid(@PathVariable("mId") String mId){
        //将信息写入model表中
        Boolean flag = iModelAdminService.deleteModel(mId);
        if(flag){
            //将信息写入modelInfo表中
            Result result = iInfoAdminService.deleteModelInfo(mId);
            return result;
        }
        return Result.fail(false,messageConstant.DELETE_ERROR);
    }
}
